<?php
/**
 * @author wyl
 * @date 2015年7月28日15:22:21
 */
Yii::import('application.components.widgets.CMenuPlus');
class CMenuNavPlus extends CMenuPlus{

	public $menuItemClass 		= 'menu-item dropdown';
	public $menuLinkClass		= 'menu-link';
	public $subMenuItemClass 	= 'menu-item menu-item-sub';
	public $subMenuClass 		= 'menu-sub dropdown-menu';
	public $isSubMenu 			= false;
	
	public function init(){
		parent::init();
		
		$this->htmlOptions['class'] = 'nav nav-pills menu menu-nav';

		$this->itemCssClass = $this->menuItemClass;
		$this->submenuHtmlOptions['class'] = $this->subMenuClass;
		
		
		Yii::app()->clientScript->registerScript('menu-nav', "
			$('.menu-nav .dropdown').on({ 
				'mouseover':function(){
					$(this).addClass('open');
				},
				'mouseout':function(){
					$(this).removeClass('open');
				}
			});
		");
		
	}
	
	public function subMenu($isSubMenu = null){
		if ($isSubMenu !== null) {
			$this->isSubMenu = $isSubMenu;
		}
		if ($this->isSubMenu) {
			$this->itemCssClass = $this->subMenuItemClass;
		}else{
			$this->itemCssClass = $this->menuItemClass;
		}
		return $this->isSubMenu;
	}
	
	protected function renderMenuItem($item)
	{
		
		$isSubMenu = $this->subMenu();
		if (!$isSubMenu) {
			$item['url'] = '#';
			$item['linkOptions'] = array(
					'data-toggle'	=>	'dropdown',
					'href'			=>	'#',
					'role'			=>	'button',
					'aria-haspopup'	=>	'true',
					'aria-expanded' => 	'false',
					'class'			=>	$this->menuLinkClass
			);
		}
		if(isset($item['url']))
		{
			$label=$this->linkLabelWrapper===null ? $item['label'] : CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']);
			return CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
		}
		else
			return CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
	}
	
	protected function renderMenuRecursive($items)
	{
		$count=0;
		$n=count($items);
		foreach($items as $item)
		{
				
			$count++;
			$options=isset($item['itemOptions']) ? $item['itemOptions'] : array();
			$class=array();
			if($item['active'] && $this->activeCssClass!='')
				$class[]=$this->activeCssClass;
			if($count===1 && $this->firstItemCssClass!==null)
				$class[]=$this->firstItemCssClass;
			if($count===$n && $this->lastItemCssClass!==null)
				$class[]=$this->lastItemCssClass;
			if($this->itemCssClass!==null)
				$class[]=$this->itemCssClass;
			if($class!==array())
			{
				if(empty($options['class']))
					$options['class']=implode(' ',$class);
				else
					$options['class'].=' '.implode(' ',$class);
			}
	
			echo CHtml::openTag('li', $options);
	
			$menu=$this->renderMenuItem($item);
			if(isset($this->itemTemplate) || isset($item['template']))
			{
				$template=isset($item['template']) ? $item['template'] : $this->itemTemplate;
				echo strtr($template,array('{menu}'=>$menu));
			}
			else
				echo $menu;
	
			if(isset($item['items']) && count($item['items']))
			{
				$this->subMenu(true);
				echo "\n".CHtml::openTag('ul',isset($item['submenuOptions']) ? $item['submenuOptions'] : $this->submenuHtmlOptions)."\n";
				$this->renderMenuRecursive($item['items']);
				echo CHtml::closeTag('ul')."\n";	
				$this->subMenu(false);
			}
	
			echo CHtml::closeTag('li')."\n";
		}
	}
	
}